import { createSlice } from '@reduxjs/toolkit';

export const authSlice = createSlice({
  name: 'auth',
  initialState: {
    token: localStorage.getItem('myToken') || '',
  },
  reducers: {
    setToken(state, action) {
      state.token = action.payload;
      localStorage.setItem('myToken',action.payload)
    },
    clearToken(state) {
      state.token = '';
      localStorage.removeItem('myToken')
    }
  },
});

export const { setToken, clearToken } = authSlice.actions;

export default authSlice.reducer;